package 第二季_42_synchronized和Lock的区别.代码;

import org.junit.Test;
import 第二季_35_BlockingQueue.代码.SharedData1;

/**
 * @author Administrator
 */
public class SynchronizedDemo1Test {

    private static int number = 0;

    class Demo implements Runnable{
        @Override
        public synchronized void run() {
            for(int j=0;j<10000;j++){
                add();
            }
        }

        private void add(){
            number++;
        }
    }

    @Test
    public void demo() {
        Demo demo = new Demo();
        Thread thread = new Thread(demo);
        Thread thread2 = new Thread(demo);
        Thread thread3 = new Thread(demo);
        thread.start();
        thread2.start();
        thread3.start();
        try {
            thread.join();
            thread2.join();
            thread3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程结束number:"+number);
    }
}


